package com.edushare.common.dao;

import java.util.List;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.SemesterGrades;
import com.edushare.common.vo.Users;

public class SemesterGradeDAO extends CayenneBasedDAO {
	public SemesterGradeDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}

	public void addSG(Users user, Batches batch, int semNum, double gradePoint, int credits) {
		SemesterGrades sg = getSG(user, batch, semNum);
		if (sg == null) {
			sg = new SemesterGrades();
			sg.setBatch(batch);
			sg.setUser(user);
			getDataContext().registerNewObject(sg);
		}
		sg.setCredits(credits);
		sg.setCumulativeGrade(0.0);
		sg.setGradePoint(gradePoint);
		sg.setSemesterNum(semNum);
		getDataContext().commitChanges();
	}

	public void addCG(Users user, Batches batch, double gradePoint, int credits) {
		SemesterGrades cg = getCG(user, batch);
		if(cg == null){
			cg = new SemesterGrades();
			cg.setUser(user);
			cg.setBatch(batch);
			getDataContext().registerNewObject(cg);
		}
		cg.setCumulativeGrade(gradePoint);
		cg.setCredits(credits);
		cg.setGradePoint(0.0);
		cg.setSemesterNum(-1);
		getDataContext().commitChanges();
	}

	public SemesterGrades getSG(Users user, Batches batch, int semNum) {
		List<SemesterGrades> gradeList = user.getSemGrades();
		for(SemesterGrades sg:gradeList){
			if(sg.getSemesterNum() == semNum)
				return sg;
		}
		return null;
	}

	public SemesterGrades getCG(Users user, Batches batch) {
		List<SemesterGrades> gradeList = user.getSemGrades();
		for(SemesterGrades sg:gradeList){
			if(sg.getSemesterNum() == -1)
				return sg;
		}
		return null;
	}

}
